/**
 * 报告查询页面
 * 1）在iframe中查找父页面元素的方法：
     $('#id', window.parent.document)
 */
define(['apps/utils/tools'],function (tools){
   var domId = "content",tableId = "tableId";
   var linkBean = {};
   /**
    * 根据名称改变dom结构
    */
   function accordingName(obj,key,linkArr){
   	     var text = $(obj).text(),linkArrLen = linkArr.length,textLen = text.length;
         for(var x=0;x<linkArrLen;x++ ){
              	var linkText  =  linkArr[x]["text"],linkId = linkArr[x]["id"],
              	indexI = text.indexOf(linkText),indexILen = linkText.length ;
                  if(indexI !=-1){
                  	var beforeSpan="",afterSpan="";
                  	  if(textLen != indexILen){
	                  	   var bf = text.substring(0,indexI),af = text.substring(indexI+indexILen,textLen);
	                  	   if(bf.length>0){
	                  	     beforeSpan = "<span>"+bf+"</span>";
	                  	   }
	                  	   if(af.length>0){
	                  	         afterSpan = "<span>"+af+"</span>" ;
	                  	   }
                  	  }else{
                  	       linkText = text;  
                  	  }
                  	  var href="javascript:parent.Utils.updateModel({type:'"+key+"',indicator:'"+linkId+"',sname:'"+linkText+"'});",
                  	  centerA = "<a title='点击可查看这个【"+linkText+"】指标的详情' style='text-decoration:blink;' id=\""+linkId+"\" href="+href+">"+linkText+"</a>";
                  	  $(obj).before(beforeSpan+centerA+afterSpan);
                  	  $(obj).remove();
                      tools.console(text +"<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>里包含【"+linkText+"】");
                  }else{
                     tools.console(text+"--里不包含【"+linkText+"】");
                  }
              }
   }
   /**
    * 遍历报告的每个自然段下面的正文
    * 因为正文都是连体且无序span的文字，所以会进行【span.size*指标数】次的循环
    * 为了节省客户端的加载时间，有必要加一些不需要的遍历条件 2014-06-07 龚佳新
    */
   function setReportContentLink($paragraph,iframe){
         $paragraph.each(function(j,obj){
              var text = $(obj).text(),pText = text.replaceAll("%","").replaceAll(",","").replaceAll("-","").replaceAll("，","").replaceAll("。","");
              if(text.indexOf("。") != -1){
                  tools.console("有句号，不去循环了------------------------------A_a【"+text+"】");
              }else if(text.indexOf("万元") != -1){
                   tools.console("有万元，不去循环了-----------------------------A_a【"+text+"】");
              }else if(!isNaN(pText)){
                   tools.console("是数字，不去循环了------------------------------A_a【"+text+"】");
              }else if(text == "年"){
                   tools.console("是年，不去循环了---------------------------------A_a【"+text+"】");
              }else if(pText == "较上年同期"){
                   tools.console("是较上年同期，不去循环了-----------------------------A_a【"+text+"】");
              }else if(pText == "减少"){
                   tools.console("是较上年同期，不去循环了-------------------------------A_a【"+text+"】");
              }else{
              	for(var key in linkBean){
                   accordingName(obj,key,linkBean[key]);
                 }
              }
       });
   }
   function setIframeHeight(bean,id){
       $("#"+id).load(function(){
	        var mainheight = $(this).contents().find("body").height()+250,
	        iframe = window.frames["reportIframeName"].document; // alert("mainheight=="+mainheight);
            $(this).height(mainheight).css("padding","10px");
            var dd07 = "div.Section1>p>span>a";
            bean["excelVersion"] = 7;
            if(bean.ext == ".doc"){
              dd07= "div.Section1>p>span>span>a";
              bean["excelVersion"] = 3;
            }
            var $sel = $(dd07,iframe),pSize = $sel.size(),
            arr = [];
            /**
             *  遍历报告的段落数， 循环次数==段落数($sel.size)*段落下span.size*指标数
             */
             $sel.each(function(i,o){
             	    var id = $(o).attr("href").replaceAll("#",""); 
                    arr.push({id:id,text:$(o).text()});
                    var $hrefO = $("a[name="+id+"]",iframe),
                    $paragraph = $hrefO.parent().next().children();
                    pSize+= $paragraph.size();
                    setReportContentLink($paragraph,iframe);
             });
           var $paragraphHeader = $("p[class=MsoNormal]",iframe).eq(1).children();//span[lang=EN-US]
            setReportContentLink($paragraphHeader,iframe);
            pSize+= $paragraphHeader.size();
            tools.console("此报告共有"+pSize+"个自然段！总共经历了【"+(pSize*Utils.reportJsonSize)+"】次循环！");//Utils.reportJsonSize
            tools.unmask();
	   }); 
  }
  
    
    /**
     * 导航条事件绑定
     */
   /* function navEventBind(){
    	alert("tools.navEventBind");
      $("li[level=2]").unbind(); 
      $("li[level=2]").bind("click",function(){
      	  $("#main1").fadeIn("slow").siblings().fadeOut("fast");
      	//  $("#listIdView").hide();
      	  $("body").eq(0).css("overflow","auto");
      	  tools.levelNav(Utils.bean);
      });
   }*/
   /**
    * 显示iframe页面信息
    */
   function changeIframe(bean){//tools.getBasePathUrl()
   	var id = "reportIframeId";
	   var url =tools.getBasePathUrl()+"resources/upload/"+bean.path+bean.sname+".html";
	   if(url !=  $("#"+id).attr("src")){
	     $("#"+id).attr("src",url);
	     setIframeHeight(bean,id);
	   }
	   tools.otherDimShow();
	   tools.levelNav(bean);
	   tools.navEventBind();
 }
  function getActiveReport(){
    return $("tr[class=activeReport]").eq(0);
  }
 
   /**
   * 报告选中事件  单选
   */
   function reportEventBind(){
   	var ids = ["yulan","download"];
      tools.tableRowClick();  
      $("#"+ids[0]).bind("click",function(){
      	  tools.mask();
      	  var $o = getActiveReport();
      	   var bean = {path:$o.attr("path"),sname:$o.attr("sname"),ext:$o.attr("ext"),level:3,scode:$o.attr("sname")};
         // var bean = {path:$(this).attr("path"),sname:$(this).attr("sname"),ext:$(this).attr("ext"),level:3,scode:$(this).attr("sname")};
          changeIframe(bean);
      });
       $("#"+ids[1]).bind("click",function(){
      	       tools.mask();
      	       var $o = getActiveReport();
      	       var path =$o.attr("path"),sname=$o.attr("sname"),ext=$o.attr("ext"),
               href=$("#"+ids[1]).attr("src")+path+sname+ext;
               window.location.href = href;
               tools.unmask();
      	  });
   }
   /**
    * index.cnbi成功事件
    */
   function success(msg){
		tools.levelNav(Utils.bean);
		$("#"+domId).empty().append(msg);
		reportEventBind();
		tools.viewChange();
		tools.dimComponentClick(Utils.dimMapJson);
		tools.bindPanelHeaderDbClick();
		 linkBean = Utils.reportJson ;
   }
  /** 
   * 初始方法
   */
  function init (){
  	  tools.eventBind({domId:domId,tableId:tableId,success:success});
   }
  return {
    init: init
  }
});